package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前加入到购物车中的商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);// 拷贝属性
        //只能查询自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());// 设置当前登录的微信用户的id

        // 判断商品是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            // 如果已经存在，只需要将数量加一
            shoppingCart = shoppingCartList.get(0);// shoppingCartList集合中只会有一条数据或者没有
            // 更新数量
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {
            // 如果不存在，则需要插入一条购物车数据
            // 判断当前购物车的数据是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                // 添加到购物车的数据是菜品
                Dish dish = dishMapper.getById(dishId);// 通过菜品id获取菜品对象
                shoppingCart.setName(dish.getName());// 菜品名称
                shoppingCart.setImage(dish.getImage());// 菜品图片路径
                shoppingCart.setAmount(dish.getPrice());// 菜品价格
            } else {
                // 添加到购物车的数据是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());// 通过套餐id获取套餐对象
                shoppingCart.setName(setmeal.getName());// 套餐名称
                shoppingCart.setImage(setmeal.getImage());// 套餐图片路径
                shoppingCart.setAmount(setmeal.getPrice());// 套餐价格
            }
            shoppingCart.setNumber(1);// 数量设置为1
            shoppingCart.setCreateTime(LocalDateTime.now());// 设置创建时间为当前系统时间
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();// 获取当前微信登录用户的id
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();// 获取当前微信登录用户的id
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车的一个商品
     *
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 设置查询条件，查询当前登录用户的购物车数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());// 设置当前登录用户id
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if (list != null && list.size() > 0) {
            shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);// 将该商品数量减1
            if (shoppingCart.getNumber() == 0) {
                // 如果这条购物车商品数量减一之后就没有了，删除这条购物车数据
                shoppingCartMapper.sub(shoppingCart);
            } else {
                // 当前购物车这条数据减一之后还有，更新数量
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }
}
